import 'package:dio/dio.dart';
import 'package:flutter_base/bean/hot/common_website_entity.dart';
import 'package:flutter_base/bean/hot/search/search_result_entity.dart';
import 'package:flutter_base/bean/hot/search_hot_key_entity.dart';
import 'package:flutter_base/bean/knowledge/knowledge_detail_entity.dart';
import 'package:flutter_base/bean/knowledge/knowlege_entity.dart';

import '../bean/home/home_article_list_entity.dart';
import '../bean/home/home_banner_entity.dart';
import '../bean/login/user_info_entity.dart';
import '../bean/mine/check_app_update_entity.dart';
import '../bean/mine/collect_list_entity.dart';
import '../net/http_helper.dart';
import 'dio_instance.dart';

///
/// @DIR_PATH：lib/bean
/// @TIME：2024/6/7 18:43
/// @AUTHOR：starr
///
///
class Api {
  static Api instance = Api._();

  Api._();

  //获取首页banner数据
  Future<List<HomeBannerEntity?>?> getBannerData() async {
    var rsp = await DioInstance.getInstance().get(path: HttpHelper.BANNER);
    HomeBannerListEntity bannerData = HomeBannerListEntity.fromJson(rsp);
    return bannerData.bannerList;
  }

  //获取首页文章列表
  Future<List<HomeArticleListDatas>?> getHomeListData(String pageCount) async {
    var json = await DioInstance.getInstance().get(path: "article/list/${pageCount}/json");
    HomeArticleListEntity articleData = HomeArticleListEntity.fromJson(json);
    return articleData.datas;
  }

  //获取首页置顶数据
  Future<List<HomeArticleListDatas>?> getHomeTopListData() async {
    var json =
        await DioInstance.getInstance().get(path: HttpHelper.HOME_TOP_LIST);
    HomeTopHomeArticleListEntity topListData =
        HomeTopHomeArticleListEntity.fromJson(json);
    return topListData.topList;
  }

  //常用网站
  Future<List<CommonWebsiteEntity>?> getCommonWebsiteData() async {
    var json = await DioInstance.getInstance().get(path: "friend/json");
    CommonWebsiteListData websiteData = CommonWebsiteListData.fromJson(json);
    return websiteData.list;
  }
  //搜索热词网站
  Future<List<SearchHotKeyEntity>?> getSearchHotKeyData() async {
    var json = await DioInstance.getInstance().get(path: "hotkey/json");
    SearchHotKeyListData hotKeyData = SearchHotKeyListData.fromJson(json);
    return hotKeyData.list;
  }

  //注册
  Future<dynamic> register(
      {String? name, String? password, String? rePassword}) async {
    dynamic response =
        await DioInstance.getInstance().post(path: "user/register", params: {
      "username": name,
      "password": password,
      "repassword": rePassword,
    });
    return response;
  }

  //登录
  //user/login
  Future<UserInfoEntity> login({String? name, String? password}) async {
    var response =
        await DioInstance.getInstance().post(path: "user/login", params: {
      "username": name,
      "password": password,
    });
    return UserInfoEntity.fromJson(response);
  }

  //获取体系数据 tree/json

  Future<List<KnowlegeData>?> getKnowledgeListData() async {
    var response = await DioInstance.getInstance().get(path: "tree/json");
    KnowledgeListData knowlegeData = KnowledgeListData.fromJson(response);
    return knowlegeData.knowledgeList;
  }
  
  //收藏站内文章
  Future<bool?> collect(String articleId) async{
    var json = await DioInstance.getInstance().post(path: "lg/collect/${articleId}/json");
    if(json == true && json is bool){
      print("收藏 $json");
      return true;
    }
    return false;
  }

  //取消收藏
//lg/uncollect_originId/2333/json
  Future<bool?> unCollect(String articleId) async{
    var json = await DioInstance.getInstance().post(path: "lg/uncollect_originId/$articleId/json");
    if(json == true && json is bool){
      print("取消收藏 $json");
      return true;
    }
    return false;
  }

  //登出操作
//user/logout/json
  Future<bool?> exitLogin() async{
    var json = await DioInstance.getInstance().get(path: "user/logout/json");
    if(json == true && json is bool){
      print("退出登录 $json");
      return true;
    }
    return false;
  }

  //知识体系下的文章
  //article/list/0/json?cid=60
  Future getKnowledgeDetail(int pageCount,int treeId)async{
    var json = await DioInstance.getInstance().get(path: "article/list/$pageCount/json",params:{"cid":treeId});
    KnowledgeDetailEntity listData = KnowledgeDetailEntity.fromJson(json);
    return listData.datas;
  }

  //搜索
  Future<List<SearchResultDatas>?> search(String keyword)async{
    var json = await DioInstance.getInstance().post(path: "article/query/0/json",params: {"k":keyword});
    SearchResultEntity searchList = SearchResultEntity.fromJson(json);
    return searchList.datas;
  }

  //收藏列表
  Future<List<CollectListDatas>?> getCollectList(int pageCount) async {
    var json = await DioInstance.getInstance()
        .get(path: "lg/collect/list/$pageCount/json");
    CollectListEntity collectList = CollectListEntity.fromJson(json);
    return collectList.datas;
  }

  //我的收藏页，取消收藏
  //lg/uncollect/2805/json
  Future<bool?> unMyCollect(String articleId) async {
    var json = await DioInstance.getInstance()
        .post(path: "lg/uncollect/$articleId/json", params: {"originId": -1});
    if (json == true && json is bool) {
      print("取消收藏 $json");
      return true;
    }
    return false;
  }


  //检查app版本是否有更新
  //https://www.pgyer.com/apiv2/app/check
  Future<CheckAppUpdateData> checkAppUpdate() async{
    var json = await DioInstance.getInstance().changeBaseUrl("https://www.pgyer.com/")
        .post(
          path: "apiv2/app/check",
          params: {"_api_key":"e37969d742fb5c1f1eee813481f409d8","appKey":"e531ad80fcbef36794a7f93ffc8ee672"});
    DioInstance.getInstance().changeBaseUrl("https://www.wanandroid.com/");
    CheckAppUpdateEntity update = CheckAppUpdateEntity.fromJson(json);
    return update.data;
  }
}
